#include <iostream>
#include <string>
using namespace std;

//1类的定义
class Box
{
public:
    double lenght;
    double width;
    double height;
public:
    Box() {
        cout << "调用构造函数！" <<endl;
    }
    ~Box() {
        cout << "调用析构函数！" <<endl;
    }
};

//继承相关
class Shape
{
public:
    void setWidth(int w)
    {
        width = w;
    }

    void setHeight(int h)
    {
        height = h;
    }

protected:
    int width;
    int height;
};
class Rectangle: public Shape
{
public:
    int getArea()
    {
        return (width*height);
    }
};
//抽象
class Adder{
public:
    // 构造函数
    Adder(int i = 0)
    {
        total = i;
    }
    // 对外的接口
    void addNum(int number)
    {
        total += number;
    }
    // 对外的接口
    int getTotal()
    {
        return total;
    };
private:
    // 对外隐藏的数据
    int total;
};
//命名空间，防止重名
// 第一个命名空间
namespace first_space{
    void func(){
        cout << "Inside first_space" << endl;
    }
}
// 第二个命名空间
namespace second_space{
    void func(){
        cout << "Inside second_space" << endl;
    }
}
//模板，就是泛型
template <typename T>
inline T const& Max (T const& a, T const& b)
{
    return a < b ? b:a;
}
int main()
{
    Box b1;
    double volume = 0.0;

    b1.height = 5.0;
    b1.lenght = 6.0;
    b1.width = 7.0;

    volume = b1.height * b1.width * b1.lenght;
    cout << "体积" << volume << endl;

    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(7);
    cout << "面积" <<rect.getArea()<<endl;

    Adder a;
    a.addNum(10);
    a.addNum(30);
    cout << a.getTotal() <<endl;

    //new 动态分配堆内存，delete动态删除堆内存
    double* pvalue  = NULL; // 初始化为 null 的指针
    pvalue  = new double;   // 为变量请求内存

    *pvalue = 29494.99;     // 在分配的地址存储值
    cout << "Value of pvalue : " << *pvalue << endl;

    delete pvalue;         // 释放内存
    int **p;
    int i,j;   //p[4][8]
    //开始分配4行8列的二维数据
    p = new int *[4];
    for(i=0;i<4;i++){
        p[i]=new int [8];
    }

    for(i=0; i<4; i++){
        for(j=0; j<8; j++){
            p[i][j] = j*i;
        }
    }
    //打印数据
    for(i=0; i<4; i++){
        for(j=0; j<8; j++)
        {
            if(j==0) cout<<endl;
            cout<<p[i][j]<<"\t";
        }
    }
    //开始释放申请的堆
    for(i=0; i<4; i++){
        delete [] p[i];
    }
    delete [] p;

    Box* myBoxArray = new Box[4];

    delete [] myBoxArray; // 删除数组

    // 调用第一个命名空间中的函数
    first_space::func();

    // 调用第二个命名空间中的函数
    second_space::func();

    int i = 39;
    int j = 20;
    cout << "Max(i, j): " << Max(i, j) << endl;

    double f1 = 13.5;
    double f2 = 20.7;
    cout << "Max(f1, f2): " << Max(f1, f2) << endl;

    string s1 = "Hello";
    string s2 = "World";
    cout << "Max(s1, s2): " << Max(s1, s2) << endl;

    return 0;
}
